<template>

  <view>
  <!-- 使用自定义组件库 -->
    <my-search @click="gotoSearch"></my-search>
    
    <view class="scroll-view-container">
      <!-- 左侧的滑动区域 -->
      <scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh+'px'}" >
        <view :class="{'left-scroll-view-item':true,'active':index===active} " v-for="(item,index) in cateList"
          :key="index" @click="setActive(index)">
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view scroll-y="true" class="right-scroll-view" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <view class="cate-lv2" v-for="(item2,index2) in cateLevel2" :key="index2">
          <!-- 二级分类标题 -->
          <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
          <!-- 动态渲染3级分类的列表数据 -->
          <view class="cate-lv3-list">
            <!-- 3级分类item项 -->
            <view class="cate-lv3-item" v-for="(item3,index3) in item2.children" :key="index3" @click="goGoodsList(item3)">
              <image :src="item3.cat_icon" mode="widthFix"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>

    </view>
  </view>

</template>

<script>
  export default {
    data() {
      return {
        //当前设备可用高度
        wh: 0,
        cateList: [],
        active: 0,
        cateLevel2: [],
        scrollTop:0
      };
    },
    onLoad() {
      const systemInfo = uni.getSystemInfoSync();
      // console.log(systemInfo);
      this.wh = systemInfo.windowHeight-50

      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 给二级分类赋值
        this.cateLevel2 = res.message[0].children
      },
      setActive(index) {
        this.active = index
        // 给二级分类重新赋值
        this.cateLevel2 = this.cateList[index].children
        // 竖向滚动条位置，左侧项切换，右侧数据置顶展示
        this.scrollTop=this.scrollTop===0?1:0
      },
      // 跳转到商品列表页面
      goGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+item.cat_id
        })
      },
      gotoSearch(){
        // console.log('ccc');
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    },
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;
    background-color: #ffffff;
    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        text-align: center;
        font-size: 28rpx;
        line-height: 120rpx;
        background-color: #f7f7f7;

        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            content: ''; //content: '' 确保伪元素存在。
            position: absolute;
            display: block; //display: block 使得伪元素成为一个块级元素，可以设置高度和宽度。
            height: 80rpx;
            width: 4rpx;
            top: 50%;
            /* 垂直居中 */
            left: 0%;
            transform: translateY(-50%);
            /* 垂直居中调整 */
            background-color: #C00000;

          }
        }
      }
    }

    .cate-lv2-title {
      font-size: 28rpx;
      font-weight: bold;
      text-align: center;
      padding: 30rpx 0;
    }

    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;
      // justify-content: space-around;
      .cate-lv3-item {
        width: 33.3%;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-bottom: 20rpx;
 

        image {
          // width: 60px;
          width: 140rpx;
        }

        text {
          font-size: 24rpx;
        }
      }
    }

  }
</style>